package com.ymatou.iapi.livebuyer.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.GetNotSpeekingListBean;
import com.ymatou.iapi.livebuyer.parameter.NotSpeakingItemDto;
import com.ymatou.iapi.livebuyer.service.GetNotSpeekingListCall;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.SpeakingNotAllowed;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*查询云通讯全局禁言
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetNotSpeekingList{
	private static GetNotSpeekingListBean getnotspeekinglistBean;
	private static GetNotSpeekingListCall getnotspeekinglistCall;
	LiveRoomWapper LiveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询云通讯全局禁言");
	}

	@Before
	public void caseUp() {
		getnotspeekinglistBean=new GetNotSpeekingListBean();
		getnotspeekinglistCall=new GetNotSpeekingListCall();
		LiveRoomWapper=new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getnotspeekinglistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB() throws InterruptedException {
		Thread.sleep(1000);
		 List<SpeakingNotAllowed> dbinfos = LiveRoomWapper.selectSpeakingNotAllowed(getnotspeekinglistBean.getUserId(),null);
		 if(getnotspeekinglistBean.getLiveId()==null||getnotspeekinglistBean.getLiveId()==0) {
			 Logger.verifyEquals(dbinfos.size(), getnotspeekinglistCall.getJsonObject().get("data").getAsJsonObject().get("count").getAsInt(), "验证条数");
		 }
		 ArrayList<NotSpeakingItemDto> NotSpeakingItemDtos = getnotspeekinglistCall.getNotSpeakingItemDtos();
		 int i=0;
		 if(getnotspeekinglistBean.getPageIndex()!=null&&getnotspeekinglistBean.getPageIndex()>1) {
			 i=(getnotspeekinglistBean.getPageIndex()-1)*getnotspeekinglistBean.getPageSize();
		 }
		 for (NotSpeakingItemDto notSpeakingItemDto : NotSpeakingItemDtos) {
			Logger.verifyEquals(notSpeakingItemDto.getNoSpeakingId(), dbinfos.get(i).getId(), "验证id");
			//10% 的抽样检查+必然检查第一条
			if(i%10==1) {
				Logger.verifyEquals(notSpeakingItemDto.getOperator(), dbinfos.get(i).getOperator(), "验证Opeator");
				Logger.verifyEquals(notSpeakingItemDto.getReason(), dbinfos.get(i).getReason(), "验证Reason");
				Logger.verifyEquals(notSpeakingItemDto.getUserId(), dbinfos.get(i).getUserId(), "验证UserId");
				Logger.verifyEquals(YMTDateUtil.getDate(notSpeakingItemDto.getBeginTime(),YMTDateUtil.YMDHMS),  dbinfos.get(i).getBeginTime(), "验证BeginTime");
				Logger.verifyEquals(YMTDateUtil.getDate(notSpeakingItemDto.getEndTime(),YMTDateUtil.YMDHMS),  dbinfos.get(i).getEndTime(), "验证EndTime");
				Logger.verifyEquals(YMTDateUtil.getDate(notSpeakingItemDto.getOperatorTime(),YMTDateUtil.YMDHMS),  dbinfos.get(i).getUpdateTime() ,"验证OpeatorTime");			
			}
			i++;
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetNotSpeekingList_001() {
		Logger.start(true,"查询云通讯全局禁言成功,封禁中");
		try {
			Integer userId=1;
			LiveRoomCallService.SetNotSpeeking(userId,100,0);
			Thread.sleep(1000);
			getnotspeekinglistBean.setUserId(userId);
			getnotspeekinglistCall.setData(getnotspeekinglistBean);
			getnotspeekinglistCall.callService();			
			Logger.verifyEquals("200", getnotspeekinglistCall.getString("code"), "验证返回");
			verifyDB();
			Logger.verifyEquals("封禁中",getnotspeekinglistCall.getNotSpeakingItemDtos().get(0).getStateText(),"验证封禁中状态");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetNotSpeekingList_002() {
		Logger.start(true,"不输入userid查询全部");
		try {
			getnotspeekinglistBean.setPageIndex(1);
			getnotspeekinglistBean.setPageSize(30);
			getnotspeekinglistCall.setData(getnotspeekinglistBean);
			getnotspeekinglistCall.callService();			
			Logger.verifyEquals("200", getnotspeekinglistCall.getString("code"), "验证返回");
			verifyDB();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetNotSpeekingList_003() {
		Logger.start(true,"不输入pageIndex默认1");
		try {
			getnotspeekinglistBean.setPageSize(30);
			getnotspeekinglistCall.setData(getnotspeekinglistBean);
			getnotspeekinglistCall.callService();			
			Logger.verifyEquals("200", getnotspeekinglistCall.getString("code"), "验证返回");
			verifyDB();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetNotSpeekingList_004() {
		Logger.start(true,"不输入pageSize默认30");
		try {
			getnotspeekinglistBean.setPageIndex(1);
			getnotspeekinglistCall.setData(getnotspeekinglistBean);
			getnotspeekinglistCall.callService();			
			Logger.verifyEquals("200", getnotspeekinglistCall.getString("code"), "验证返回");
			verifyDB();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetNotSpeekingList_005() {
		Logger.start(true,"翻页查询成功");
		try {
			getnotspeekinglistBean.setPageIndex(2);
			getnotspeekinglistBean.setPageSize(30);
			getnotspeekinglistCall.setData(getnotspeekinglistBean);
			getnotspeekinglistCall.callService();			
			Logger.verifyEquals("200", getnotspeekinglistCall.getString("code"), "验证返回");
			verifyDB();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetNotSpeekingList_006() {
		Logger.start(true,"pageSize超过100失败");
		try {
			getnotspeekinglistBean.setPageIndex(2);
			getnotspeekinglistBean.setPageSize(101);
			getnotspeekinglistCall.setData(getnotspeekinglistBean);
			getnotspeekinglistCall.callService();			
			Logger.verifyEquals("100", getnotspeekinglistCall.getString("code"), "验证返回");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetNotSpeekingList_007() {
		Logger.start(true,"查询云通讯全局禁言成功，人工解禁");
		try {
			Integer userId=1;
			LiveRoomCallService.SetNotSpeeking(userId,100,0);
			LiveRoomCallService.DisableNotSpeaking(userId);
			Thread.sleep(1000);
			getnotspeekinglistBean.setUserId(userId);
			getnotspeekinglistCall.setData(getnotspeekinglistBean);
			getnotspeekinglistCall.callService();			
			Logger.verifyEquals("200", getnotspeekinglistCall.getString("code"), "验证返回");
			verifyDB();
			Logger.verifyEquals("人工解禁",getnotspeekinglistCall.getNotSpeakingItemDtos().get(0).getStateText(),"验证人工解禁");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetNotSpeekingList_008() {
		Logger.start(true,"查询云通讯全局禁言成功，自动解禁");
		try {
			Integer userId=1;
			LiveRoomCallService.SetNotSpeeking(userId,1,0);
			Thread.sleep(2000);
			getnotspeekinglistBean.setUserId(userId);
			getnotspeekinglistCall.setData(getnotspeekinglistBean);
			getnotspeekinglistCall.callService();			
			Logger.verifyEquals("200", getnotspeekinglistCall.getString("code"), "验证返回");
			verifyDB();
			Logger.verifyEquals("自动解禁",getnotspeekinglistCall.getNotSpeakingItemDtos().get(0).getStateText(),"验证自动解禁");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetNotSpeekingList_009() {
		Logger.start(true,"查询云通讯直播禁言成功");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			initcreatebean.getLiveDto().setGroupId(LiveRoomCallService.CreateGroup("SU"+TestDataManager.getRandomNum(5),initcreatebean.getLiveDto().getUserId()));
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			int liveid = livedto.getId();
			//需要tx云存在的用户
			Integer userId = 24636166;
			LiveRoomCallService.SetNotSpeeking(userId,100,liveid);
			Thread.sleep(1000);
			getnotspeekinglistBean.setUserId(userId);
			getnotspeekinglistBean.setLiveId(liveid);
			getnotspeekinglistCall.setData(getnotspeekinglistBean);
			getnotspeekinglistCall.callService();			
			Logger.verifyEquals("200", getnotspeekinglistCall.getString("code"), "验证返回");
			verifyDB();
			Logger.verifyEquals("封禁中",getnotspeekinglistCall.getNotSpeakingItemDtos().get(0).getStateText(),"验证封禁中状态");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}